package top.melody.ecommerce.platform.service.security.impl;

import org.springframework.stereotype.Service;
import top.melody.ecommerce.common.util.page.PageRows;
import top.melody.ecommerce.common.util.page.Pagination;
import top.melody.ecommerce.platform.EcommerceException;
import top.melody.ecommerce.platform.api.domain.system.SecurityUser;
import top.melody.ecommerce.platform.api.service.SecurityUserService;
import top.melody.ecommerce.platform.dal.system.SecurityUserMapper;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2016/10/27 13:47
 * @mail: blentle.huan.ren@gmail.com
 * @description:
 * @since:1.0
 */
@Service
public class SecurityUserServiceImpl implements SecurityUserService {

    @Resource
    private SecurityUserMapper securityUserMapper;

    @Override
    public SecurityUser getSecurityUser(String userId) throws EcommerceException {
        try {
            return securityUserMapper.get(userId);
        } catch (Exception e) {
            throw new EcommerceException("execute mapper query exception", e);
        }
    }

    @Override
    public PageRows<SecurityUser> querySecurityUserListByPage(SecurityUser queryEntity, Pagination page) throws EcommerceException {
        PageRows<SecurityUser> pageRows = new PageRows<SecurityUser>();
        try {
            List<SecurityUser> userList = securityUserMapper.queryByPage(queryEntity, page);
            int count = securityUserMapper.getCount(queryEntity);
            if (page == null)
                page = new Pagination(count, 1, count);
            else
                page.setTotalRows(count);
            pageRows.setPage(page);
            pageRows.setRows(userList);
            return pageRows;
        } catch (Exception e) {
            throw new EcommerceException("execute mapper query exception", e);
        }
    }

    @Override
    public boolean modifySecurityUser(SecurityUser user) throws EcommerceException {
        try {
            return securityUserMapper.updateByPrimaryKey(user) > 0;
        } catch (Exception e) {
            throw new EcommerceException("execute mapper modify exception", e);
        }
    }
}
